import validateUtil from './validateUtil'
import config from '../config/config'
require('es6-promise').polyfill()
export default class storeUtil {
  /** ***authority**/
  static setAuthority(obj) {
    this.setStore(config.constantConfig.authorityConstant, obj)
    this.setDefaultStatusMenu()
  };

  static getAuthority() {
    return this.getStore(config.constantConfig.authorityConstant) || {}
  };

  static getAuthorityByKey(key) {
    const result = this.getAuthority()
    return result[key]
  };

  static getToken() {
    return this.getAuthorityByKey(config.constantConfig.userTokenConstant)
  };

  static removeAuthority() {
    this.removeStore(config.constantConfig.authorityConstant)
  };

  static removeAuthorityByKey(key) {
    const json = this.getAuthority()
    delete json[key]
    this.setAuthority(json)

  };

  /** ********************设置Menu***************************/
  static setMenu(obj) {
    this.setStore(config.constantConfig.menuConstant, obj)
  };

  static getMenu() {
    return this.getStore(config.constantConfig.menuConstant) || {}
  };

  static getMenuByKey(key) {
    const result = this.getMenu()
    return result[key]
  };

  static removeMenu() {
    this.removeStore(config.constantConfig.menuConstant)
  };

  static removeMenuByKey(key) {
    const json = this.getMenu()
    delete json[key]
    this.setMenu(json)
  };

  static setFirstMenu(obj) {
    const json = this.getMenu()
    json.FirstLay = obj
    this.setMenu(json)
  };

  static getFirstMenu() {
    const json = this.getMenu()
    return json.FirstLay
  };

  static deleteFirstMenu() {
    this.removeMenuByKey('FirstLay')
  };

  static setSecondMenu(obj) {
    const json = this.getMenu()
    json.SecondLay = obj
    this.setMenu(json)
  };

  static getSecondMenu() {
    const json = this.getMenu()
    return json.SecondLay
  };

  static setStatusMenu(value) {
    const json = this.getMenu()
    json.Status = value
    this.setMenu(json)
  };

  static setDefaultStatusMenu() {
    const json = this.getMenu()
    json.Status = false
    this.setMenu(json)
  };

  static setRefreshStatusMenu() {
    const json = this.getMenu()
    json.Status = true
    this.setMenu(json)
  };

  static getStatusMenu() {
    const json = this.getMenu()
    return json.Status
  };

  static setFirstMenuId(id) {
    const json = this.getMenu()
    json.FirstLayId = id
    this.setMenu(json)
  };

  static getFirstMenuId() {
    const json = this.getMenu()
    return json.FirstLayId
  };

  /** 基础**/
  /** 以下为Store基础设置(不允许修改)**/
  static setStore(name, obj) {
    if (validateUtil.isNull(obj)) { return }
    window.localStorage.setItem(name, JSON.stringify(obj))
  };

  static removeStore(name) {
    window.localStorage.removeItem(name)
  };

  static getStoreStr(name) {
    return window.localStorage.getItem(name)
  };

  static getStore(name) {
    const data = window.localStorage.getItem(name)
    if (validateUtil.isNull(data)) { return '' }
    return JSON.parse(window.localStorage.getItem(name))
  };
}
